<?php

/**
 * @Desc:转账（账户间）
 * @author dasheng@baofu.com(大圣)
 * @datetime 2025/4/6 19:49
 */

require_once "../../Init/init.php";

use App\baocaitong3\Entity\ContentBodyEntity as ContentBody;
use App\baocaitong3\Entity\HeaderEntity as Header;
use App\baocaitong3\Entity\PostParmEntity as PostParm;
use App\baocaitong3\Entity\TransAccountEntity as TransAccount;
use App\baocaitong3\SecurityUtil\NewRsaUtil;
use App\baocaitong3\Util\HttpClient;
use App\baocaitong3\Util\Log;
use App\baocaitong3\Util\Tools;

$ServiceTp = "BCT3-3001-001-01";
$transSerialNo = Tools::getTsnOderid("TSN");
Log::EchoFormat("商户流水号：".$transSerialNo);


$tracce = new TransAccount();
$tracce->version = "1.0.0";
$tracce->payeeNo = "CM610000000000174898";
$tracce->payerNo = "CM610000000000174078";
$tracce->transSerialNo = $transSerialNo;
$tracce->accountType = "BALANCE";
$tracce->dealAmount = "10.56";

$headerinfo = new Header();
$headerinfo->terminalId = $GLOBALS["TerminalId"];
$headerinfo->memberId = $GLOBALS["MemberId"];
$headerinfo->serviceTp = $ServiceTp;
$headerinfo->verifyType = "1";

$bodycontent = new ContentBody();
$bodycontent->header = $headerinfo;
$bodycontent->body = $tracce;

$ContentStr = json_encode($bodycontent);
Log::EchoFormat("JSON序列号：".$ContentStr);
$enContent = NewRsaUtil::encryptByPfx($ContentStr,$GLOBALS["PFX_PATH"],$GLOBALS["KeyPwd"]);

$postParm = new PostParm();
$postParm->memberId = $GLOBALS["MemberId"];
$postParm->terminalId = $GLOBALS["TerminalId"];
$postParm->content = $enContent;


$ResultPost  = HttpClient::Post($postParm,"https://vgw.baofoo.com/union-gw/api/".$ServiceTp."/transReq.do");
Log::EchoFormat("返回参数：".$ResultPost);
if(empty($ResultPost)) throw new Exception("请求返回异常");

if(strpos($ResultPost,"header") != false)
{
    $resulterror = json_decode($ResultPost,true);
    var_dump($resulterror);
}else{
    $DePostString = NewRsaUtil::decrptByPub($ResultPost,$GLOBALS["CER_PATH"]);
    Log::EchoFormat("解密返回：".$DePostString);
    if(empty($DePostString))  throw new Exception("解密空值，请检查证书是否正常");
    if(strpos($DePostString,"header"))
    {
        $contenBe = json_decode($DePostString,true);
        if($contenBe["header"]["sysRespCode"] === "S_0000")
        {
            Log::EchoFormat("业务层结果:".json_encode($contenBe["body"]));
            if($contenBe["body"]["retCode"] === 1){
                Log::EchoFormat("结果:state状态:".$contenBe["body"]["state"]);
            }else{
                Log::EchoFormat("操作失败:".json_encode($contenBe["body"],JSON_UNESCAPED_UNICODE));
            }
        }
    }
}
